<?php
defined('_JEXEC') or die('Restricted access');
$img_path = JURI::root(true).'/images/fileupload/';
?>
<div class='tab_content' id="tab_list_repair">
	<div class='container-fluid margin10'>
		<div class='row-fluid'>
			<div class='widget widget-padding span12'>
				<div class='widget-header'>
					<i class="icon-list-alt"></i>
					<h5>Danh sách sửa chữa / bảo trì</h5>
				</div>
				<div class='widget-body'>
					<div class='row-fluid'>
						<div class='span12'>
							<?php if(Utility::checkAccess("core.create", $this->canDo)): ?>
							<a class='btn btn-primary' href='#create_repair'>
								<i class='icon-plus icon-white'></i>
								<?php echo JText::_("COM_KETNOI_CREATEBTN") ?>
							</a>
							<?php endif; ?>
							<?php if(Utility::isAdmin()): ?>
							<a onclick="showModalPermission('repair')" class='btn btn-danger'>
								<i class='icon-lock icon-white'></i>
								Phân quyền
							</a>
							<?php endif; ?>
						</div>
					</div>
					
					<div class="row-fluid">
						<div class='span12'>
							<div id='grid_repair' class='margin10'></div>
						</div>
					</div>
					
				</div>
				
			</div>
		</div>
	</div>
</div>

<script>
    jQuery(document).ready(function(){    
        jQuery("#grid_repair").kendoGrid({
			dataSource: {
				transport: {
					read:{
						url:"index.php?option=com_ketnoi&view=repair&tmpl=component",
						dataType:"json"
					}	
				},
				schema: {
					data:"results",
					total:"total",
					model: {
                        fields: {
							counteruse: { type: "number" }
	                    }
	                }
				},
				pageSize: 10,
				serverPaging: true
			},
			pageable:{
				pageSizes: true,
				messages: {
					display: "{0} - {1} <?php echo JText::_('COM_KETNOI_MESSAGEOF') ?> {2} <?php echo JText::_('COM_KETNOI_MESSAGEDATA') ?>",
					empty: "",
					page: "Page",
					of: "of {0}",
					itemsPerPage: "<?php echo JText::_('COM_KETNOI_MESSAGEITEMPERPAGE') ?>",
					first: "<?php echo JText::_('COM_KETNOI_GOTOFIRSTPAGE') ?>",
					previous: "<?php echo JText::_('COM_KETNOI_GOTOPREVIOUSPAGE') ?>",
					next: "<?php echo JText::_('COM_KETNOI_GOTONEXTPAGE') ?>",
					last: "<?php echo JText::_('COM_KETNOI_GOTOLASTPAGE') ?>",
					refresh: "Refresh"	
				}
			},
			scrollable:false,
			autoBind: false,
			detailTemplate: kendo.template($("#template").html()),
			detailInit: detailInit,
			columns: [
                {
					field:"serial",
					title:"Serial"
				},
				{
					field:"tenmay",
					title:"Tên máy"
				},
				{
					field:"tenkhachhang",
					title:"Tên khách hàng"
				},
				{
					field:"counteruse",
					title:"Số counter",
					format:"{0:n0}"
				},
				{
					field:"datecreate",
					title:"Ngày tạo",
					template:kendo.template(jQuery('#datecreate_tpl').html())
				}
            ],
            dataBound:function(){
				if(this._data.length == 0)
				{
					var colspan = jQuery("#grid_linhkien").find('table thead tr th').length;
                    jQuery("#grid_linhkien").find('table tbody').append("<tr><td colspan='"+colspan+"'><?php echo JText::_('COM_KETNOI_EMPTYDATA') ?></td></tr>");
				}

				//make column on reponsive
				AddCellLabel("grid_linhkien");	
            }
		});
    })
    
    function detailInit(e) {
    	var detailRow = e.detailRow;

        	detailRow.find(".tabstrip").kendoTabStrip({
            	animation: {
            		sopen: { effects: "fadeIn" }
            	}
           }); 

        	detailRow.find(".grid_linhkien_nested").kendoGrid({
        		dataSource: {
					transport: {
						read:{
							url:"index.php?option=com_ketnoi&view=repair&layout=linhkiennested&tmpl=raw",
							dataType:"json"
						}	
					},
					schema: {
						data:"results",
						total:"total",
						model: {
	                        fields: {
								sodungluongbaohanh: { type: "number" },
								tuoitho: { type: "number" }
	                        }
	                    }
					},
					serverPaging: true,
					serverFiltering: true,
	                pageSize:5,
	                filter: { field: "c.id", operator: "eq", value: e.data.id }
				},
				pageable:{
					pageSizes: true,
					messages: {
						display: "{0} - {1} <?php echo JText::_('COM_KETNOI_MESSAGEOF') ?> {2} <?php echo JText::_('COM_KETNOI_MESSAGEDATA') ?>",
						empty: "",
						page: "Page",
						of: "of {0}",
						itemsPerPage: "<?php echo JText::_('COM_KETNOI_MESSAGEITEMPERPAGE') ?>",
						first: "<?php echo JText::_('COM_KETNOI_GOTOFIRSTPAGE') ?>",
						previous: "<?php echo JText::_('COM_KETNOI_GOTOPREVIOUSPAGE') ?>",
						next: "<?php echo JText::_('COM_KETNOI_GOTONEXTPAGE') ?>",
						last: "<?php echo JText::_('COM_KETNOI_GOTOLASTPAGE') ?>",
						refresh: "Refresh"	
					}
				},
				scrollable:false,
				
				columns: [
	                {
						field:"malinhkien",
						title:"Mã linh kiện"
					},
					{
						field:"tenlinhkien",
						title:"Tên linh kiện"
					},
					{
						field:"sodungluongbaohanh",
						title:"Dung lượng bảo hành",
						format:"{0:n0}"
					},
					{
						field:"sothangbaohanh",
						title:"Tháng bảo hành"
					},
					{
						field:"tuoitho",
						title:"Tuổi thọ",
						format:"{0:n0}"
					},
					{
						field:"tennhacungcap",
						title:"Nhà cung cấp",
						template:"${manhacungcap} - ${tennhacungcap}"
					},
					{
						title:"Tình trạng",
						template:kendo.template($("#tinhtrang_tpl").html()),
						width:200
					}
	            ],
	            dataBound:function(){	
					//make column on reponsive
					//AddCellLabel("grid_linhkien_nested");	
	            }
        	}); 


        	detailRow.find(".grid_nhanvien_nested").kendoGrid({
        		dataSource: {
					transport: {
						read:{
							url:"index.php?option=com_ketnoi&view=repair&layout=nhanviennested&tmpl=raw",
							dataType:"json"
						}	
					},
					schema: {
						data:"results",
						total:"total"
					},
					serverPaging: true,
					serverFiltering: true,
	                pageSize:5,
	                filter: { field: "e.repair_id", operator: "eq", value: e.data.id }
				},
				pageable:{
					pageSizes: true,
					messages: {
						display: "{0} - {1} <?php echo JText::_('COM_KETNOI_MESSAGEOF') ?> {2} <?php echo JText::_('COM_KETNOI_MESSAGEDATA') ?>",
						empty: "",
						page: "Page",
						of: "of {0}",
						itemsPerPage: "<?php echo JText::_('COM_KETNOI_MESSAGEITEMPERPAGE') ?>",
						first: "<?php echo JText::_('COM_KETNOI_GOTOFIRSTPAGE') ?>",
						previous: "<?php echo JText::_('COM_KETNOI_GOTOPREVIOUSPAGE') ?>",
						next: "<?php echo JText::_('COM_KETNOI_GOTONEXTPAGE') ?>",
						last: "<?php echo JText::_('COM_KETNOI_GOTOLASTPAGE') ?>",
						refresh: "Refresh"	
					}
				},
				scrollable:false,
				
				columns: [
	                {
						field:"ma",
						title:"Mã nhân viên"
					},
					{
						field:"hoten",
						title:"Tên nhân viên"
					}
	            ],
	            dataBound:function(){	
					//make column on reponsive
					//AddCellLabel("grid_linhkien_nested");	
	            }
        	});


        	detailRow.find(".grid_maloi_nested").kendoGrid({
        		dataSource: {
					transport: {
						read:{
							url:"index.php?option=com_ketnoi&view=repair&layout=maloinested&tmpl=raw",
							dataType:"json"
						}	
					},
					schema: {
						data:"results",
						total:"total"
					},
					serverPaging: true,
					serverFiltering: true,
	                pageSize:5,
	                filter: { field: "rm.repair_id", operator: "eq", value: e.data.id }
				},
				pageable:{
					pageSizes: true,
					messages: {
						display: "{0} - {1} <?php echo JText::_('COM_KETNOI_MESSAGEOF') ?> {2} <?php echo JText::_('COM_KETNOI_MESSAGEDATA') ?>",
						empty: "",
						page: "Page",
						of: "of {0}",
						itemsPerPage: "<?php echo JText::_('COM_KETNOI_MESSAGEITEMPERPAGE') ?>",
						first: "<?php echo JText::_('COM_KETNOI_GOTOFIRSTPAGE') ?>",
						previous: "<?php echo JText::_('COM_KETNOI_GOTOPREVIOUSPAGE') ?>",
						next: "<?php echo JText::_('COM_KETNOI_GOTONEXTPAGE') ?>",
						last: "<?php echo JText::_('COM_KETNOI_GOTOLASTPAGE') ?>",
						refresh: "Refresh"	
					}
				},
				scrollable:false,
				
				columns: [
	                {
						field:"ma",
						title:"Mã lỗi"
					},
					{
						field:"ten",
						title:"Tên lỗi"
					}
	            ],
	            dataBound:function(){	
					//make column on reponsive
					//AddCellLabel("grid_linhkien_nested");	
	            }
        	});
	}
</script>

 <script type="text/x-kendo-template" id="datecreate_tpl">
    #
		var date = data.datecreate.split("-");
		date = date[2]+'-'+date[1]+'-'+date[0];
	#
	<span> #= date # </span>
</script>

<script type="text/x-kendo-template" id="template">
	<div class="tabstrip">
		<ul>
			<li class="k-state-active">
				Thông tin linh kiện
            </li>
			<li>
				Danh sách nhân viên
            </li>
			<li>
				Danh sách mã lỗi
            </li>
        </ul>
        <div>
       		<div class="row-fluid">
				<div class="grid_linhkien_nested"></div>
			</div>
        </div>
		<div>
       		<div class="row-fluid">
				<div class="grid_nhanvien_nested"></div>
			</div>
        </div>
		<div>
       		<div class="row-fluid">
				<div class="grid_maloi_nested"></div>
			</div>
        </div>
	</div>

</script>

<script type="text/x-kendo-template" id="tinhtrang_tpl">
	#
		var percent = calculateTinhtrang(data.closing,data.tuoitho);
		if(parseInt(data.closing) < 0) data.closing = 0;
		percent = 100 - percent;
		var percent_class = getPercentClass(percent);	
	#
	<div class="progress custom progress-striped #: percent_class # active">
		<div class="bar" style="width: #: percent #%;"></div>
		<div class='barnumber'>#: number_format(percent) #% - #: number_format(data.closing) #</div>
	</div>
</script>
